//
//  SRMonth+Cate.m
//  SportRing
//
//  Created by mykoma on 14/11/1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRMonth+Cate.h"
#import "SRDB.h"

@implementation SRMonth (Cate)

+ (SRMonth *)month
{
    __block SRMonth * month = nil;
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         month = [SRMonth MR_createEntity];
     }];
    return month;
}

- (void)configAttributesWithBlock:(void(^)(SRMonth * aSelf))block
{
    [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:^(NSManagedObjectContext *localContext)
     {
         block(self);
     }];
}

@end

@implementation SRMonth (Find)

+ (NSArray *)findAllMonthsForMe:(SRMe *)me
{
    return [me.months allObjects];
}

+ (SRMonth *)findMonthForMonthStr:(NSString *)monthStr forMe:(SRMe *)me
{
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"title == %@ AND me.userName == %@", monthStr, me.userName];
    return [SRMonth MR_findFirstWithPredicate:predicate];
}

+ (SRMonth *)findMonthCINEForMonthStr:(NSString *)monthStr forMe:(SRMe *)me
{
    SRMonth * month = [SRMonth findMonthForMonthStr:monthStr forMe:me];
    if (month == nil) {
        month = [SRMonth month];
        [month configAttributesWithBlock:^(SRMonth *aSelf) {
            aSelf.title = monthStr;
            aSelf.me = me;
        }];
    }
    return month;
}

+ (SRMonth *)findMonthForDate:(NSDate *)date forMe:(SRMe *)me
{
    NSString * month = [date formattedBy_yyyyMM];
    return [SRMonth findMonthForMonthStr:month forMe:me];
}

+ (SRMonth *)findMonthCINEForDate:(NSDate *)date forMe:(SRMe *)me
{
    NSString * month = [date formattedBy_yyyyMM];
    return [SRMonth findMonthCINEForMonthStr:month forMe:me];
}

- (CGFloat)processOfTasks
{
    __block NSInteger allCount = 0;
    __block NSInteger doneCount = 0;
    
    NSArray * days = [SRDay findDaysForMonth:self];
    [days enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        SRDay * day = obj;
        SRSportTask * task = day.task;
        allCount += task.totalCounter.intValue;
        doneCount += task.completedCounter.intValue;
    }];
    
    if (self.task && self.task.totalCounter.intValue > allCount) {
        return (float)self.task.completedCounter.integerValue / (float)self.task.totalCounter.integerValue;
    }
    
    return allCount == 0 ? 0 : (float)doneCount / (float)allCount;
}

- (NSInteger)countOfSteps
{
    __block NSInteger count = 0;
    
    NSArray * days = [SRDay findDaysForMonth:self];
    [days enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        SRDay * day = obj;
        count += [day countOfSteps];
    }];
    
    return count;
}

- (NSInteger)levelOfSport
{
    NSInteger count = [self countOfSteps];
    if (count > 6000) {
        return 5;
    }
    else if (count > 4800)
    {
        return 4;
    }
    else if (count > 3600)
    {
        return 3;
    }
    else if (count > 2400)
    {
        return 2;
    }
    else if (count > 1200)
    {
        return 1;
    }
    return 0;
}

- (CGFloat)countOfDistance
{
    __block NSInteger count = 0;
    
    NSArray * days = [SRDay findDaysForMonth:self];
    [days enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        SRDay * day = obj;
        count += [day countOfDistance];
    }];
    
    return count;
}

- (CGFloat)countOfCalorie
{
    __block NSInteger count = 0;
    
    NSArray * days = [SRDay findDaysForMonth:self];
    [days enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        SRDay * day = obj;
        count += [day countOfCalorie];
    }];
    
    return count;
}

- (CGFloat)averageStepsOfDay
{
    NSArray * days = [SRDay findDaysForMonth:self];
    if (days.count == 0) {
        return 0;
    }
    else
    {
        NSInteger allStep = [self countOfSteps];
        return (float)allStep / days.count;
    }
}

- (CGFloat)averageDistanceOfDay
{
    NSArray * days = [SRDay findDaysForMonth:self];
    if (days.count == 0) {
        return 0;
    }
    else
    {
        NSInteger allDistance = [self countOfDistance];
        return (float)allDistance / days.count;
    }
}

- (CGFloat)averageCalorieOfDay
{
    NSArray * days = [SRDay findDaysForMonth:self];
    if (days.count == 0) {
        return 0;
    }
    else
    {
        NSInteger allCalorie = [self countOfCalorie];
        return (float)allCalorie / days.count;
    }
}

@end